<?php
/**
 * Clase Base DP para BaseImStockMovimDP
 *
 * @package    
 * @subpackage BaseImStockMovimDP
 * @author     hans
 * @version    SVN:: BaseImStockMovimDP.class.php,v 1.1 2012-03-18 20:28:48 Hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/common/arrayClass.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/BN/ImStockMovimBN.class.php');


abstract class BaseImStockMovimDP extends ImStockMovimBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof ImStockMovimBN )
    {
      $o_ImStockMovimBN = $bSetDefault;
      if($o_ImStockMovimBN->getMovimId()) : $this->setMovimId($o_ImStockMovimBN->getMovimId()); endif;
      if($o_ImStockMovimBN->getDocId()) : $this->setDocId($o_ImStockMovimBN->getDocId()); endif;
      if($o_ImStockMovimBN->getDocNum()) : $this->setDocNum($o_ImStockMovimBN->getDocNum()); endif;
      if($o_ImStockMovimBN->getAlmId()) : $this->setAlmId($o_ImStockMovimBN->getAlmId()); endif;
      if($o_ImStockMovimBN->getArtId()) : $this->setArtId($o_ImStockMovimBN->getArtId()); endif;
      if($o_ImStockMovimBN->getSecuencia()) : $this->setSecuencia($o_ImStockMovimBN->getSecuencia()); endif;
      if($o_ImStockMovimBN->getMovFecha()) : $this->setMovFecha($o_ImStockMovimBN->getMovFecha()); endif;
      if($o_ImStockMovimBN->getIngSal()) : $this->setIngSal($o_ImStockMovimBN->getIngSal()); endif;
      if($o_ImStockMovimBN->getMovTipo()) : $this->setMovTipo($o_ImStockMovimBN->getMovTipo()); endif;
      if($o_ImStockMovimBN->getCant()) : $this->setCant($o_ImStockMovimBN->getCant()); endif;
      if($o_ImStockMovimBN->getCantAnt()) : $this->setCantAnt($o_ImStockMovimBN->getCantAnt()); endif;
      if($o_ImStockMovimBN->getCosto()) : $this->setCosto($o_ImStockMovimBN->getCosto()); endif;
      if($o_ImStockMovimBN->getDocReferenTipo()) : $this->setDocReferenTipo($o_ImStockMovimBN->getDocReferenTipo()); endif;
      if($o_ImStockMovimBN->getDocReferenNro()) : $this->setDocReferenNro($o_ImStockMovimBN->getDocReferenNro()); endif;
      if($o_ImStockMovimBN->getCreaUsr()) : $this->setCreaUsr($o_ImStockMovimBN->getCreaUsr()); endif;
      if($o_ImStockMovimBN->getCreaFecha()) : $this->setCreaFecha($o_ImStockMovimBN->getCreaFecha()); endif;
      if($o_ImStockMovimBN->getModifUsr()) : $this->setModifUsr($o_ImStockMovimBN->getModifUsr()); endif;
      if($o_ImStockMovimBN->getModifFecha()) : $this->setModifFecha($o_ImStockMovimBN->getModifFecha()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT IF(MAX(stkm_id),(MAX(stkm_id)+1), 1) AS Maximo FROM im_stock_movim;";
      $result = mysql_query($sql);
      $row = mysql_fetch_array($result);
      $this->setMovimId($row[0]);

      if($this->getMovimId()){
        $a_Fields = array();

        if($this->getMovimId()) : $a_Fields['stkm_id'] = ($this->getMovimId()); endif;
        if($this->getDocId()) : $a_Fields['stkm_docId'] = ($this->getDocId()); endif;
        if($this->getDocNum()) : $a_Fields['stkm_docNum'] = ($this->getDocNum()); endif;
        if($this->getAlmId()) : $a_Fields['stkm_almId'] = ($this->getAlmId()); endif;
        if($this->getArtId()) : $a_Fields['stkm_artId'] = ($this->getArtId()); endif;
        if($this->getSecuencia()) : $a_Fields['stkm_secuencia'] = ($this->getSecuencia()); endif;
        if($this->getMovFecha()) : $a_Fields['stkm_movFecha'] = ($this->getMovFecha()); endif;
        if($this->getIngSal()) : $a_Fields['stkm_ing_sal'] = ($this->getIngSal()); endif;
        if($this->getMovTipo()) : $a_Fields['stkm_movTipo'] = ($this->getMovTipo()); endif;
        if($this->getCant()) : $a_Fields['stkm_cant'] = ($this->getCant()); endif;
        if($this->getCantAnt()) : $a_Fields['stkm_cantAnt'] = ($this->getCantAnt()); endif;
        if($this->getCosto()) : $a_Fields['stkm_costo'] = ($this->getCosto()); endif;
        if($this->getDocReferenTipo()) : $a_Fields['stkm_docReferenTipo'] = ($this->getDocReferenTipo()); endif;
        if($this->getDocReferenNro()) : $a_Fields['stkm_docReferenNro'] = ($this->getDocReferenNro()); endif;
        if($this->getCreaUsr()) : $a_Fields['stkm_crea_usr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['stkm_crea_fecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['stkm_modif_usr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['stkm_modif_fecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO im_stock_movim (".$sStringField.") VALUES (".$sStringValue.");";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getMovimId())
      {
        $a_Fields = array();

        if($this->getMovimId()) : $a_Fields['stkm_id'] = ($this->getMovimId()); endif;
        if($this->getDocId()) : $a_Fields['stkm_docId'] = ($this->getDocId()); endif;
        if($this->getDocNum()) : $a_Fields['stkm_docNum'] = ($this->getDocNum()); endif;
        if($this->getAlmId()) : $a_Fields['stkm_almId'] = ($this->getAlmId()); endif;
        if($this->getArtId()) : $a_Fields['stkm_artId'] = ($this->getArtId()); endif;
        if($this->getSecuencia()) : $a_Fields['stkm_secuencia'] = ($this->getSecuencia()); endif;
        if($this->getMovFecha()) : $a_Fields['stkm_movFecha'] = ($this->getMovFecha()); endif;
        if($this->getIngSal()) : $a_Fields['stkm_ing_sal'] = ($this->getIngSal()); endif;
        if($this->getMovTipo()) : $a_Fields['stkm_movTipo'] = ($this->getMovTipo()); endif;
        if($this->getCant()) : $a_Fields['stkm_cant'] = ($this->getCant()); endif;
        if($this->getCantAnt()) : $a_Fields['stkm_cantAnt'] = ($this->getCantAnt()); endif;
        if($this->getCosto()) : $a_Fields['stkm_costo'] = ($this->getCosto()); endif;
        if($this->getDocReferenTipo()) : $a_Fields['stkm_docReferenTipo'] = ($this->getDocReferenTipo()); endif;
        if($this->getDocReferenNro()) : $a_Fields['stkm_docReferenNro'] = ($this->getDocReferenNro()); endif;
        if($this->getCreaUsr()) : $a_Fields['stkm_creaUsr'] = ($this->getCreaUsr()); endif;
        if($this->getCreaFecha()) : $a_Fields['stkm_creaFecha'] = ($this->getCreaFecha()); endif;
        if($this->getModifUsr()) : $a_Fields['stkm_modifUsr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['stkm_modifFecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "UPDATE im_stock_movim SET ".$sStringField." WHERE 1 AND stkm_id='".$this->getMovimId()."';";
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getMovimId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "DELETE FROM im_stock_movim WHERE 1 AND stkm_id='".$this->getMovimId()."';"; 
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vMovimId)
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT stkm_id, stkm_docId, stkm_docNum, stkm_almId, stkm_artId, stkm_secuencia, stkm_movFecha, stkm_ing_sal, stkm_movTipo, stkm_cant, stkm_cantAnt, stkm_costo, stkm_docReferenTipo, stkm_docReferenNro, stkm_creaUsr, stkm_creaFecha, stkm_modifUsr, stkm_modifFecha FROM im_stock_movim WHERE 1 AND stkm_id='".$vMovimId."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_ImStockMovimDP = new ImStockMovimBN();
    $o_ImStockMovimDP->setMovimId($o_propel->stkm_id);
    $o_ImStockMovimDP->setDocId($o_propel->stkm_docId);
    $o_ImStockMovimDP->setDocNum($o_propel->stkm_docNum);
    $o_ImStockMovimDP->setAlmId($o_propel->stkm_almId);
    $o_ImStockMovimDP->setArtId($o_propel->stkm_artId);
    $o_ImStockMovimDP->setSecuencia($o_propel->stkm_secuencia);
    $o_ImStockMovimDP->setMovFecha($o_propel->stkm_movFecha);
    $o_ImStockMovimDP->setIngSal($o_propel->stkm_ing_sal);
    $o_ImStockMovimDP->setMovTipo($o_propel->stkm_movTipo);
    $o_ImStockMovimDP->setCant($o_propel->stkm_cant);
    $o_ImStockMovimDP->setCantAnt($o_propel->stkm_cantAnt);
    $o_ImStockMovimDP->setCosto($o_propel->stkm_costo);
    $o_ImStockMovimDP->setDocReferenTipo($o_propel->stkm_docReferenTipo);
    $o_ImStockMovimDP->setDocReferenNro($o_propel->stkm_docReferenNro);
    $o_ImStockMovimDP->setCreaUsr($o_propel->stkm_creaUsr);
    $o_ImStockMovimDP->setCreaFecha($o_propel->stkm_creaFecha);
    $o_ImStockMovimDP->setModifUsr($o_propel->stkm_modifUsr);
    $o_ImStockMovimDP->setModifFecha($o_propel->stkm_modifFecha);
    return $o_ImStockMovimDP;
  }
}